package com.yangjie.autotest.component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.inject.Inject;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.springframework.stereotype.Component;

import com.yangjie.autotest.component.xml.Components;
import com.yangjie.log.LoggerIF;

@Component
public class ComponentReader {

	@Inject
	private LoggerIF logger = null;
	
	
	public LoggerIF getLogger() {
		return logger;
	}


	public void setLogger(LoggerIF logger) {
		this.logger = logger;
	}


	public Components readComponents(String fileName) {
		logger.debug("read componetns from file:" + fileName);
		try {
			File file = new File(fileName);
			JAXBContext jaxbContext = JAXBContext.newInstance(Components.class);

			Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
//			TestCases testcases = (TestCases) jaxbUnmarshaller.unmarshal(file);
			JAXBElement<Components> root = jaxbUnmarshaller.unmarshal(new StreamSource(new FileInputStream(file)), Components.class); 			
			
			Components components = root.getValue();
			logger.debug(components.toString());
			
			return components;
		} catch (JAXBException e) {
			e.printStackTrace();
			return null;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
